package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"week53/client/client"
	"week53/client/request"
	"week53/common/proto/user"
)

func Register(c *gin.Context) {
	var data request.User
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	register, err := client.Register(c, &user.RegisterReq{
		Name:     data.Name,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": register,
	})
}

func Login(c *gin.Context) {
	var data request.User
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	login, err := client.Login(c, &user.LoginReq{
		Name:     data.Name,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": login,
	})
}

func AddClassPlan(c *gin.Context) {
	userId := c.GetUint("userId")

	var data request.AddClassPlan
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	plan, err := client.AddClassPlan(c, &user.AddClassPlanReq{
		UserId:  int32(userId),
		ClassId: int32(data.ClassId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": plan,
	})
}

func DelClassPlan(c *gin.Context) {
	userId := c.GetUint("userId")

	var data request.DelClassPlan
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	plan, err := client.DelClassPlan(c, &user.DelClassPlanReq{
		Id:     int32(data.Id),
		UserId: int32(userId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": plan,
	})
}
